package com.jandar.table.config;

import java.util.ArrayList;
import java.util.List;

/**
 * Table类。
 * 
 * @author pqj
 * @version 2009-6-10
 */
public class Table {
	/** 部门 */
	private Department department = null;
	
	/** 表名 */
	private Name name = null;

	/** ID */
	private List<Id> id = null;

	/** 字段 */
	private List<Column> column = null;

	public List<Column> getColumn() {
		return column;
	}

	public void setColumn(List<Column> column) {
		this.column = column;
	}

	public List<Id> getId() {
		return id;
	}

	public void setId(List<Id> id) {
		this.id = id;
	}

	public Name getName() {
		return name;
	}

	public void setName(Name name) {
		this.name = name;
	}

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public Column[] getAllColumn() {
		if (id == null || column == null) {
			return null;
		}
		int size = id.size() + column.size();
		Column[] allColumn = new Column[size];
		int index = 0;
		for (int i=0; i < id.size(); ++i) {
			allColumn[index++] = id.get(i);
		}
		for (int i=0; i < column.size(); ++i) {
			allColumn[index++] = column.get(i);
		}
		return allColumn;
	}
	
	public int getIdCount() {
		return (id == null) ? 0 : id.size();
	}
	
	public int getColumnCount() {
		return (column == null) ? 0 : column.size();
	}
	
	public int getAllColumnCount() {
		return getIdCount() + getColumnCount();
	}

//	public List<Id> getEffectiveId() {
//		List<Id> effectiveId = new ArrayList<Id>();
//		if (id != null) {
//			for (Id item : id) {
//				if (item.getType() != ColumnTypeInfo.VIRTUAL) {
//					effectiveId.add(item);
//				}
//			}
//		}
//		
//		return (effectiveId.size() == 0) ? null : effectiveId;
//	}
	
	public List<Column> getEffectiveColumn() {
		List<Column> effectiveColumn = new ArrayList<Column>();
		if (column != null) {
			for (Column item : column) {
				if (item.getType() != ColumnTypeInfo.VIRTUAL) {
					effectiveColumn.add(item);
				}
			}
		}
		
		return (effectiveColumn.size() == 0) ? null : effectiveColumn;
	}
	
	public String toString() {
		StringBuilder buf = new StringBuilder();
		buf.append("\t<table>\n");
		if (department != null) {
			buf.append("\t\t").append(department.toString()).append("\n");
		}
		buf.append("\t\t").append(name.toString()).append("\n");
		for (int i = 0; i < id.size(); ++i) {
			buf.append("\t\t").append(id.get(i).toString()).append("\n");
		}
		if (column != null) {
			for (int i = 0; i < column.size(); ++i) {
				buf.append("\t\t").append(column.get(i).toString()).append("\n");
			}
		}
		buf.append("\t</table>");
		return buf.toString();
	}
}
